package cn.school.mybatis.service;

import cn.school.mybatis.dao.StudentDao;
import cn.school.mybatis.pojo.Student;
import cn.school.mybatis.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//业务逻辑bean
//服务层,调用dao方法
public class StudentService {

    public final static StudentService STUDENT_SERVICE = new StudentService();

    //模糊查tel数量
    public boolean findTel(String tel) {
        Map map = new HashMap<>();
        map.put("telephone", tel);
        return this.findTotal(map)>0;
    }
    //模糊查weixin数量
    public boolean findWechat(String wechat) {
        Map map = new HashMap<>();
        map.put("wechat", wechat);
        return this.findTotal(map)>0;
    }

    //增加
    public int add(Student student) {
        SqlSession session = MyBatisUtil.getSqlSession();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            return dao.add(student);
        } finally {
            session.commit();
            MyBatisUtil.close(session);
        }
    }

    //删除
    public int delete(int stuno) {
        SqlSession session = MyBatisUtil.getSqlSession();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            return dao.delete(stuno);
        } finally {
            session.commit();
            MyBatisUtil.close(session);
        }
    }

    //修改
    public int update(Student student) {
        SqlSession session = MyBatisUtil.getSqlSession();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            return dao.update(student);
        } finally {
            session.commit();
            MyBatisUtil.close(session);
        }
    }

    //查单个
    public Student get(int stuno) {
        SqlSession session = MyBatisUtil.getSqlSession();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            return dao.get(stuno);
        } finally {
            MyBatisUtil.close(session);
        }
    }

    //模糊查数量
    public int findTotal(Map map) {
        SqlSession session = MyBatisUtil.getSqlSession();
        try {
            StudentDao studentDao = session.getMapper(StudentDao.class);
            int count = studentDao.findTotal(map);
            return count;
        } finally {
            MyBatisUtil.close(session);
        }
    }

    //模糊分页查询
    public List<Student> search(int pageNo, int pageSize, Map<String, Object> condition) {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao;
        int offset = (pageNo - 1) * pageSize;
        offset = Math.max(offset, 0);
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", offset);
        map.put("pageSize", pageSize);
        map.put("orderBy", "stuno asc");
        if (condition.size() > 0) {
            for (String key : condition.keySet()) {
                map.put(key, condition.get(key));
            }
        }
        try {
            studentDao = session.getMapper(StudentDao.class);
            List<Student> list = studentDao.search(map);
            return list;
        } finally {
            MyBatisUtil.close(session);
        }
    }

}
